package edu.umassd.raddacl.dataset;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import edu.umassd.raddacl.Cluster;
import edu.umassd.raddacl.Observation;
import edu.umassd.raddacl.calc.ClusterMetrics;

/**
 * Internal class used to create child clusters from a parent.
 * 
 * @author Dan Avila
 * 
 */
@Component
@Scope("prototype")
public class Split
{
	@Autowired
	private ClusterMetrics metrics;

	private Cluster cluster;
	private int iteration;

	public boolean isValidObservation(Observation obs)
	{
		for (int i = 0; i < obs.size(); i++)
		{
			Observation min = null;
			Observation max = null;

			if (isOn(i))
			{
				min = metrics.getCentroid(cluster);
				max = metrics.getMax(cluster);
			}
			else
			{
				min = metrics.getMin(cluster);
				max = metrics.getCentroid(cluster);
			}

			if (obs.get(i) < min.get(i) || obs.get(i) > max.get(i))
			{
				return false;
			}
		}

		return true;
	}

	public void setCluster(Cluster cluster)
	{
		this.cluster = cluster;
	}

	public void setIteration(int iteration)
	{
		this.iteration = iteration;
	}

	private boolean isOn(int value)
	{
		int i = iteration >> value;
		i = i & 1;

		return i == 1;
	}
}
